package com.graduation.mybatis.controller;

import com.graduation.mybatis.bean.NewsDTO;
import com.graduation.mybatis.bean.UserLike;
import com.graduation.mybatis.bean.UserLikeDTO;
import com.graduation.mybatis.service.UserLikeService;
import jakarta.annotation.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


/**
 * (UserLike)表控制层
 *
 * @author makejava
 * @since 2025-05-01 17:25:28
 */
@RestController
@RequestMapping("userLike")
public class UserLikeController {
    /**
     * 服务对象
     */
    @Resource
    private UserLikeService userLikeService;

    /**
     * 分页查询
     *
     * @param userLike 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
//    @GetMapping
//    public ResponseEntity<Page<UserLike>> queryByPage(UserLike userLike, PageRequest pageRequest) {
//        return ResponseEntity.ok(this.userLikeService.queryByPage(userLike, pageRequest));
//    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<UserLike> queryById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(this.userLikeService.getById(id));

    }

    /**
     * 新增数据
     *
     * @param userLike 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<UserLike> add(UserLike userLike) {
        return userLikeService.save(userLike) ?
                ResponseEntity.ok(userLike) :
                ResponseEntity.internalServerError().build();
    }

    /**
     * 编辑数据
     *
     * @param userLike 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<UserLike> edit(UserLike userLike) {
        return userLikeService.updateById(userLike) ?
                ResponseEntity.ok(userLike) :
                ResponseEntity.internalServerError().build();
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Long id) {
        return ResponseEntity.ok(this.userLikeService.removeById(id));
    }

    /**
     * 添加点赞
     * @param dto
     * @return
     */
    @PostMapping("/add")
    public ResponseEntity<Map<String, Object>> addLike(@RequestBody UserLikeDTO dto) {
        Map<String, Object> response = new HashMap<>();
        userLikeService.addLike(dto.getUserId(), dto.getNewsId());
        response.put("message",  "点赞成功");
        response.put("success", true);
        return ResponseEntity.ok(response);
    }

    /**
     * 取消点赞
     * @param dto
     * @return
     */
    @PostMapping("/cancel")
    public ResponseEntity<Map<String,Object>> cancelLike(@RequestBody UserLikeDTO dto) {
        userLikeService.removeLike(dto.getUserId(), dto.getNewsId());
        Map<String, Object> response = new HashMap<>();
        response.put("message",  "取消成功");
        response.put("success", true);
        return ResponseEntity.ok(response);
    }

}

